<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    * Ajax Add Node
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 22.05.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * class ajaxAddNode
    ****
    * This class will create a new node via an AJAX method
    ****
    * @package ajax
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 22.05.2005
    * @copyright essentialmind.com 2005
    ***/
    class ajaxAddNode extends ajaxAction{

        public function process(){
            //retrieve data
            $title  = $_REQUEST['title'];
            $type   = (int)$_REQUEST['ntype'];
            $parent = (int)$_REQUEST['parent'];
            $orderPos = (int)$_REQUEST['porderPos'];
       
            //there is a need for an external library that creates
            //valid URLs out of textual names
            $myTranslit = new translitWrap();
                       
            //create tree handle
            $tree = new tree($this->db,'structure LEFT JOIN objectmap ON (structure.id = objectmap.nodeid)',$this->log,'id, url, level, title');
           
            //fix inputs
            $title  = $this->unescape($title);
			
            //prepare variables
            $title  = htmlspecialchars($title);         //title of the new node
            
            //some bogus check
            if(empty($title)){
                $title = 'n/a';
            }
            
            //think about the url
            $url    = $myTranslit->UrlTranslit($title); //url of the new node
            
            if(empty($url)){
                $url = 'n_a';
            }
			
            //try to add the new node
            try{
                //get the codeset pattern for the current action
                $codeSet        = 'adminNode'.ucfirst($this->db->getOne("SELECT codeset FROM datatype WHERE id = ".$type));                
                $addController  = new $codeSet($this->db,$this->smarty,$this->log);
                $newObjectID    = $addController->addInstance();

                //log action
                $this->log->recordAction("Adding new node \"$url\" to parent \"$parent\"");
                
                //bogus check against double URL
                if($this->db->getOne("SELECT id FROM structure WHERE url = '$url' AND parent_id = $parent")){
                    $url = $url . '_' . $newObjectID;
                }
               
                //do addition
                $newNodeID = $tree->addNode($url,$parent,$title,$type);

                // set order position based on ina.ini config: enable_order_position
                if (!($orderPos == null) && $GLOBALS['_CONFIG']['enable_order_position']) {
                    $this->db->Execute("UPDATE structure SET order_pos = ". $orderPos . " WHERE id = ". $newNodeID);
                }
 
                //create a node to object mapping
                $this->db->Execute("UPDATE objectmap SET 
                                                objectid  = ".$newObjectID.",
                                                owner_uid = ".$_SESSION['admin']['id'].",
                                                owner_gid = ".$_SESSION['admin']['gid']."
                                    WHERE nodeid = ".$newNodeID);
                
                //log action
                $this->log->recordAction("Ajax: Successfully added new node \"$url\" to parent \"$parent\"");
                
            //something went wrong
            }catch(exception $e){
                //log error
                $this->log->recordError($e->getMessage());

                //do nothing
            }
            
            //do some smarty magic
            $this->smarty->assign(array('id'=>$newNodeID,'title'=>$title));
            
            //send reply to the client
			$this->r->html  = $this->smarty->preFetch('ajax'.DIRECTORY_SEPARATOR.'newFolder.html');
			$this->r->newID = $newNodeID;
        }
    }
?>